package blatt2;

import java.awt.*;
import java.awt.event.*;

public class MultiBallWorld extends Frame {
	public static void main(String[] args) {
		MultiBallWorld world = new MultiBallWorld();

		world.show();
	}

	private static final int FrameWidth = 600;
	private static final int FrameHeight = 400;
	private static int ballnumber = 12;
	private BoundedBall balls[];

	private MultiBallWorld() {
		setSize(FrameWidth, FrameHeight);
		setTitle("Multi Ball World");

		WindowListener wl = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};
		addWindowListener(wl);

		// generate balls
		balls = new BoundedBall[ballnumber];

		for (int i = 0; i < ballnumber; i++) {
			balls[i] = new BoundedBall((int) (Math.random() * FrameWidth),
					(int) (Math.random() * FrameHeight), 10, 0, FrameWidth, 0,
					FrameHeight);

			balls[i].setColor(new Color((float) Math.random(), (float) Math
					.random(), (float) Math.random()));

			balls[i].setMotion(3.0, 6.0);
		}
	}

	public void paint(Graphics g) {
		for (int i = 0; i < ballnumber; i++) {
			balls[i].paint(g);
			balls[i].move();

		}

		// check for collisions, stop balls

		for (int i = 0; i < ballnumber; i++)
			for (int j = i + 1; j < ballnumber; j++)
				if (balls[i].region().intersects(balls[j].region())) {
					balls[i].setMotion(-balls[i].xMotion(), -balls[i].yMotion());
					balls[j].setMotion(-balls[j].xMotion(), -balls[j].yMotion());
					// balls [i].setMotion(0, 0);
					// balls [j].setMotion(0, 0);
				}

		try {
			java.lang.Thread.sleep(10);
		} catch (Exception e) {
		}
		;
		repaint();
	}
}